package com.foo.ui_demo_widge;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

public class ActionModeDemo2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_mode_demo2);
        ListView lv = findViewById(R.id.iv_action_mode_demo);
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        lv.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            int num;

            @Override
            public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
                View itemview = lv.getChildAt(i - lv.getFirstVisiblePosition());
                if (b) {
                    itemview.setBackgroundColor(Color.GREEN);
                    num++;
                } else {
                    itemview.setBackgroundColor(Color.WHITE);
                    num--;
                }
                actionMode.setTitle("选中" + num + "个");
            }

            @Override
            public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                actionMode.getMenuInflater().inflate(R.menu.action_menu, menu);
                num = 0;
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode actionMode, MenuItem item) {
                int itemId = item.getItemId();
                if (itemId == R.id.menu_edit || itemId == R.id.menu_mark || itemId == R.id.menu_delete) {
                    Toast.makeText(ActionModeDemo2Activity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                    actionMode.finish();
                    return true;
                }
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode actionMode) {

            }
        });

    }
}